<%@ page import="java.util.*"%>
<%@ page import="objects.*"%>
<%@ page import="Domain.UserClient.ButtonsFunctions"%>
<%@page import="Domain.DataManager"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%
	DataManager dm = (DataManager) (session.getAttribute("dm"));
	ButtonsFunctions bf = (ButtonsFunctions) (session
			.getAttribute("bf"));

	if (dm == null || dm.getUserStatus() == DataManager.DISCONNECTED) {
		response.sendRedirect("Welcome.jsp");
	} else {
%>

<%
		String title = request.getParameter("title");
		String body = request.getParameter("body");
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="style.css" type="text/css">

<title>Edit Post</title>

<link rel="stylesheet" href="windowfiles/dhtmlwindow.css" type="text/css" />

<script type="text/javascript" src="windowfiles/dhtmlwindow.js"></script>

<script language="javascript" type="text/javascript">
		
		//Browser Support Code
		function updateUsingAjax() {
			var ajaxRequest;

			// The variable that makes Ajax possible! 
			try {
				// Opera 8.0+, Firefox, Safari 
				ajaxRequest = new XMLHttpRequest();
			} catch (e) {
				// Internet Explorer Browsers 
				try {
					ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {
						// Something went wrong 
						alert("Your browser broke!");
						return false;
					}
				}
			}
			
			// Create a function that will receive data sent from the server 
			ajaxRequest.onreadystatechange = function() {
				if (ajaxRequest.readyState == 4) {
					var response = ajaxRequest.responseText;			
					document.getElementById("numOfNotifications").innerHTML = response;
				}
			};

			var url = "GetNumOfNotifications.jsp";
			var params = "";
			ajaxRequest.open("POST", url, true);

			//Send the proper header information along with the request
			ajaxRequest.setRequestHeader("Content-type",
					"application/x-www-form-urlencoded");
			ajaxRequest.setRequestHeader("Content-length", params.length);
			ajaxRequest.setRequestHeader("Connection", "close");

			ajaxRequest.send(params);
		}

		function updateNumOfNotifications() {
			updateUsingAjax();
			setTimeout("updateNumOfNotifications()", 3000);
		}
		
		function openmypage() { //Define arbitrary function to run desired DHTML Window widget codes
			ajaxwin = dhtmlwindow.open("ajaxbox", "ajax", "GetNotification.jsp", 
					"Notification window", "width=450px,height=300px,left=300px,top=100px,resize=1,scrolling=1");
			ajaxwin.onclose = function() {
				return window.confirm("Close notification window?");
			}
		}
		
	</script>

<script language="javascript" type="text/javascript">
<!--
	function checkNotEmpty() {
		var title = document.myForm.title.value;
		var body = document.myForm.body.value;

		if (body == "")
			document.getElementById("errorLabel").innerHTML = "<font color=\"red\">you have to enter some message body</font>";
		else if (body.indexOf('&') != -1)
			document.getElementById("errorLabel").innerHTML = "<font color=\"red\">you can't enter the char '&'</font>";
		else
			parent.location = "CheckEditPost.jsp?title=" + title + "&body="
					+ body;
	}
// -->
</script>

</head>
<body>

	<table width="100%" height="100%" border="1" cellspacing="0"
		cellpadding="0">

		<!-- HEADER -->
		<tr class="header">
			<td>
				<!-- cool navigation toolbar code -->
				<ul id="navlist">
					<li id="logout"><a href="Logout.jsp"></a>
					</li>
					<li id="prev"><a
						href="ChoosePost.jsp?nameOfThread=<%out.print(dm.getCurrThread());%>"></a>
					</li>
					<li id="notifications"><a href="#" onClick="openmypage(); return false"></a>
					</li>
					<li id="notificationCounter"><center><font size=3 id="numOfNotifications"></font></center></li>
					<li id="currentForum">Current Forum is: <b><i><font
													color='#FF0000'> <%
					 	out.print(dm.getCurrForum());
					 %> </font> </i> </b>
										</li>
										<li id="currentThread"><br>Current Thread is: <b><i><font
													color='#FF0000'> <%
					 	out.print(dm.getCurrThread());
					 %> </font> </i> </b>
					</li>					
					<li id="connection">Connected as: <b><i><font
								color='#FF0000'> <%
 	out.print(dm.getCurrUser());
 %> </font> </i> </b>
					</li>
				</ul></td>
		</tr>

		<!-- BODY -->
		<tr class="body">
			<td class="forumBody">
				<center>
					<h6>
						<font color="#FFF11" size="7"><i>Edit Post</i> </font>
					</h6>
				</center>

				<FORM METHOD=POST ACTION="CheckLogin.jsp" name="myForm">
					<div align="center">
						<br> <br><b><font color="#FFFF11">The title of this message is: </font></b>
						<b><font size=4 color="#CCFFFF"><% out.print(title); %>
						</font> <br> <br><br> <b><font color="#FFFF11">Please enter the new body of
						your message:</font></b> </b><br> <br>
						<textarea cols="40" rows="5" name="body"><%out.print(body);%></textarea>
						<br> <br>

						<p id='errorLabel'></p>
						<input type="hidden" name="title" value="<%out.print(title);%>">

						<P>
							<button type="button" onClick="checkNotEmpty();">Submit</button>
						</P>
					</div>
				</FORM>
			</td>
		</tr>
	</table>
	<script language="javascript" type="text/javascript">updateNumOfNotifications();</script>
</body>
</html>
<%
	}
%>